import sys
import time


sys.path.append('../../tts/third_party/Matcha-TTS')
sys.path.append('../../tts')
sys.path.append('.')
from cosyvoice.cli.cosyvoice import CosyVoice, CosyVoice2
from cosyvoice.utils.file_utils import load_wav
import torchaudio
from gxl_ai_utils.utils import utils_file
import json

gpu_id = 7
cosyvoice = CosyVoice('/mnt/sfs/asr/ckpt/cosyvoice1/CosyVoice-300M-25Hz/CosyVoice-300M-25Hz', gpu_id=gpu_id)
prompt_speech_16k = load_wav("/mnt/sfs/asr/code/osum_xlgeng/examples/wenetspeech/tts/assert/hq_1.wav", 16000)

def token_list2wav(token_list, file_name=None):
    token_list = [int(i) for i in token_list]
    j = cosyvoice.inference_zero_shot_gxl(
        '收到好友从远方寄来的生日礼物。',
        '希望你以后能够做的比我还好呦。', prompt_speech_16k, stream=False, token_list=token_list)
    import os
    os.makedirs('data/output_data', exist_ok=True)
    # 得到一个当前的时间戳作为文件名
    timestamp = time.time()*1000
    if file_name is None:
        file_name = timestamp
    wav_path = 'data/output_data/test2cosyvoice1-25hz_{}_gxl.wav'.format(file_name)
    utils_file.makedir_for_file(wav_path)
    torchaudio.save(wav_path, j['tts_speech'],cosyvoice.sample_rate)
    return wav_path

def token_list2wav2(token_list, output_file_path):
    token_list = [int(i) for i in token_list]
    j = cosyvoice.inference_zero_shot_gxl(
        '收到好友从远方寄来的生日礼物。',
        '希望你以后能够做的比我还好呦。', prompt_speech_16k, stream=False, token_list=token_list)
    import os
    wav_path = output_file_path
    utils_file.makedir_for_file(wav_path)
    torchaudio.save(wav_path, j['tts_speech'],cosyvoice.sample_rate)
    return wav_path

